/**
 * 
 */
package com.hexacta.hrs.report.managers;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;

import org.vectrics.common.util.DateUtil;
import org.vectrics.cv.Studies;
import org.vectrics.employee.Employee;
import org.vectrics.user.ContactInfo;
import org.vectrics.user.Gender;

import com.hexacta.hrs.report.AbstractReport;
import com.hexacta.hrs.report.dtos.EmployeePersonalInfoReportDTO;
import com.hexacta.hrs.services.employee.EmployeeService;

/**
 * Clase que se encarga de generar el reporte Employee Personal Info.
 * 
 * @author mfunes
 * 
 */
public class EmployeePersonalInfoReport extends AbstractReport {

    private EmployeeService employeeService;

    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.AbstractReport#getReportData(java.util.Map)
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Collection getReportData(final Map<String, Object> filters) {
        Collection<Employee> employees = employeeService.getAll();
        employees.removeAll(employeeService.getNoLongerEmployees());
        Collection<EmployeePersonalInfoReportDTO> result = new ArrayList<EmployeePersonalInfoReportDTO>();
        String pivotAux;
        for (Employee employee : employees) {
            EmployeePersonalInfoReportDTO reportDTO = new EmployeePersonalInfoReportDTO();
            reportDTO.setBirthday(DateUtil.getFechaFormateada(employee.getPerson().getDateOfBirth()));
            pivotAux = employee.getPerson().getCuilOrCuit();
            reportDTO.setCuilCuit(pivotAux != null ? pivotAux : "----");
            reportDTO.setFirstName(employee.getPerson().getFirstName());
            reportDTO.setGender(Gender.find(employee.getPerson().getGender()).toString());
            reportDTO.setLastName(employee.getPerson().getLastName());
            ContactInfo contactInfo = employee.getPerson().getContactInfo();
            if (contactInfo != null) {
                reportDTO.setEMail(employee.getPerson().getContactInfo().getEmail());
                if (contactInfo.getHomeAddress() != null) {
                    reportDTO.setLocation(contactInfo.getHomeAddress().getLocation().getName());
                    reportDTO.setHomeAddress(contactInfo.getHomeAddress().getAddress1());
                    pivotAux = contactInfo.getHomeAddress().getPostalCode();
                    reportDTO.setPostalCode(pivotAux == null || pivotAux.length() == 0 ? "----" : pivotAux);
                } else {
                    reportDTO.setLocation("----");
                    reportDTO.setHomeAddress("----");
                    reportDTO.setPostalCode("----");
                }
                if (contactInfo.getHomePhone() != null && contactInfo.getHomePhone().getFormattedNumber() != null
                        && contactInfo.getHomePhone().getFormattedNumber().length() != 0) {
                    reportDTO.setHomePhone(contactInfo.getHomePhone().getFormattedNumber());
                } else {
                    reportDTO.setHomePhone("----");
                }
            } else {
                reportDTO.setLocation("----");
                reportDTO.setHomeAddress("----");
                reportDTO.setPostalCode("----");
                reportDTO.setHomePhone("----");
                reportDTO.setEMail("----");
            }
            if (employee.getPerson().getMaritalStatus() != null) {
                reportDTO.setMaritalStatus(employee.getPerson().getMaritalStatusDescription(Locale.getDefault()));
            }
            Studies employeeStudy = getLastStudies(employee.getPerson().getStudies());
            if (employeeStudy != null) {
                if (employeeStudy.getUniversity() != null) {
                    pivotAux = employeeStudy.getUniversity().getName();
                    reportDTO.setSchoolUniversity(pivotAux == null || pivotAux.length() == 0 ? "----" : pivotAux);
                } else if (employeeStudy.getSchool() != null) {
                    pivotAux = employeeStudy.getSchool();
                    reportDTO.setSchoolUniversity(pivotAux == null || pivotAux.length() == 0 ? "----" : pivotAux);
                }

                if (employeeStudy.getStatus() != null && employeeStudy.getStatus().length() != 0) {
                    String status = employeeStudy.getStatus();
                    reportDTO.setStatus(status.compareTo("f") == 0 ? "Completo"
                            : status.compareTo("c") == 0 ? "En curso" : "Abandonado");
                }

                // studyLevel => es studyType! secundario, posgrado,
                // universitario, etc => esta en memoria, sin tabla
                if (employeeStudy.getLevel() != null) {
                    pivotAux = employeeStudy.getLevel();
                    String levelstr = pivotAux.equals("u") ? "Universitario" : pivotAux.equals("g") ? "Postgrado"
                            : pivotAux.equals("s") ? "Secundario" : pivotAux.equals("t") ? "Terciario" : "----";
                    reportDTO.setStudyType(levelstr);
                } else {
                    reportDTO.setStudyType("----");
                }

            } else {
                reportDTO.setSchoolUniversity("----");
                reportDTO.setStatus("----");
                reportDTO.setStudyType("----");
            }
            result.add(reportDTO);
        }
        if (result.isEmpty())
            throw new RuntimeException("There is no data for this report");
        Collections.sort((List) result);
        return result;
    }

    private Studies getLastStudies(final Set<Studies> studies) {
        if (!studies.isEmpty() && studies.size() < 2)
            return studies.toArray(new Studies[] {})[0];
        Studies result = null;
        for (Studies employeeStudies : studies) {
            if (result == null) {
                result = employeeStudies;
            } else {
                if (employeeStudies.getStartYear() > result.getStartYear()) {
                    result = employeeStudies;
                }
            }
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.AbstractReport#getReportParameters()
     */
    @SuppressWarnings("unchecked")
    @Override
    protected Map getReportParameters() {
        return null;
    }

    /** {@inheritDoc} */
    public ByteArrayOutputStream createPDFReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createPDF(reportTemplateLocation, parameters, getReportData(filters));
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.Report#createXLSReport(java.lang.String,
     * java.lang.String)
     */
    public ByteArrayOutputStream createXLSReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
        return createExcel(reportTemplateLocation, parameters, getReportData(filters));
    }
}
